/********************************************************************************
 * Project Name		[JavaSE_Fundamentals]
 * File Name     	[Object.java]
 * Creation Date 	[2015-01-01]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ********************************************************************************/
package objectOriented;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author 不落的太阳(Sean Yang aka ShortPeace)
 * @version 1.0
 * @since jdk 1.8
 * 
 */
public class Object extends java.lang.Object implements Cloneable {

	private String id;
	private String name;
	private char gender;
	private Date birthday;

	public Object() {
		super();
	}

	public Object(String id) {
		this(id, "No Name", 'F', new Date());
	}

	public Object(String id, String name, char gender, Date birthday) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
		this.birthday = birthday;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getGender() {
		return gender;
	}

	public void setGender(char gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(java.lang.Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Object other = (Object) obj;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	/**
	 * 返回对象的字符串表示
	 * 
	 * @return 对象的字符串表示
	 */
	@Override
	public String toString() {
		return "id = " + id + "\tname = " + name + "\tgender = " + gender + "\tbirthday = "
				+ new SimpleDateFormat("yyyy年MM月dd日").format(birthday);
	}

	/**
	 * 垃圾回收方法
	 * 
	 * @throws Throwable
	 */
	@Override
	protected void finalize() throws Throwable {
		System.out.println("finalize method is called...");
	}

	/**
	 * clone对象
	 * 
	 * @return 克隆得到的新对象
	 */
	@Override
	protected Object clone() throws CloneNotSupportedException {
		Object cloned = new Object();
		cloned = (Object) super.clone();
		cloned.birthday = (Date) birthday.clone();
		return cloned;
	}

	public static void main(String[] args) {
		Object object1 = new Object();
		object1.setId("1");
		object1.setName("Sean");

		Object object2 = new Object();
		object2.setId("1");
		object2.setName("Sean");

		System.out.println(object1.hashCode());
		System.out.println(object2.hashCode());
		System.out.println(object1.equals(object2));
	}
}
